package com.example.myapplication;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.view.View;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class LectureActivity extends AppCompatActivity {
    private ListView lectureListView;
    private ProgressBar progressBar;
    private TextView emptyView;
    private LectureSpider lectureSpider;
    private Handler mainHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lecture);

        // 设置标题
        setTitle(R.string.lecture_title);

        // 初始化视图
        lectureListView = findViewById(R.id.lectureListView);
        progressBar = findViewById(R.id.progressBar);
        emptyView = findViewById(R.id.emptyView);
        mainHandler = new Handler(Looper.getMainLooper());
        lectureSpider = new LectureSpider();

        loadLectureData();
    }

    private void loadLectureData() {
        progressBar.setVisibility(View.VISIBLE);
        emptyView.setVisibility(View.GONE);

        new Thread(() -> {
            try {
                List<Map<String, String>> lectures = lectureSpider.fetchLectures();
                mainHandler.post(() -> {
                    progressBar.setVisibility(View.GONE);
                    if (lectures.isEmpty()) {
                        emptyView.setText(R.string.no_lectures);
                        emptyView.setVisibility(View.VISIBLE);
                        lectureListView.setVisibility(View.GONE);
                    } else {
                        emptyView.setVisibility(View.GONE);
                        lectureListView.setVisibility(View.VISIBLE);
                        updateListView(lectures);
                    }
                });
            } catch (Exception e) {
                mainHandler.post(() -> {
                    progressBar.setVisibility(View.GONE);
                    emptyView.setText(R.string.error_loading);
                    emptyView.setVisibility(View.VISIBLE);
                    lectureListView.setVisibility(View.GONE);
                });
            }
        }).start();
    }

    private void updateListView(List<Map<String, String>> lectures) {
        SimpleAdapter adapter = new SimpleAdapter(
                this,
                lectures,
                R.layout.lecture_item,
                new String[] { "title", "time", "link" },
                new int[] { R.id.textTitle, R.id.textTime, R.id.textLink });
        lectureListView.setAdapter(adapter);

        // 添加点击事件处理
        lectureListView.setOnItemClickListener((parent, view, position, id) -> {
            Map<String, String> lecture = lectures.get(position);
            String url = lecture.get("link");
            if (url != null && !url.isEmpty()) {
                if (!url.startsWith("http")) {
                    url = "https://www.swufe.edu.cn" + url;
                }
                Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
                startActivity(browserIntent);
            }
        });
    }
}
